<template>
  <a-card :body-style="{padding: '24px 32px'}" :bordered="false">
    <a-form :form="form" @submit="handleSubmit" >
      ## for(item in entity.generalList) {
        ## if(item.isId == true){continue;}
        ## if(item.name == "deleted" ){continue;}
        ## if(item.name == "createAt" ){continue;}
        ## if(item.name == "creatorId" ){continue;}
        ## if(item.name == "modifyAt" ){continue;}
        ## if(item.name == "modifierId" ){continue;}
        <a-form-item label="${item.displayName}" :labelCol="labelCol" :wrapperCol="wrapperCol" hasFeedback >
          <el-input v-decorator="[ '${item.name}', ]" ></el-input>
        </a-form-item>
      ## }
      <a-form-item v-bind="buttonCol" >
        <a-row>
          <a-col span="6">
            <a-button type="primary" html-type="submit">提交</a-button>
          </a-col>
          <a-col span="8"></a-col>
        </a-row>
      </a-form-item>
    </a-form>
  </a-card>
</template>
<script>
  import {require,pattern} from '../../utils/rules';
  import {saveOne,getDetail} from "../../api/${entity.code}";
  import pick from 'lodash.pick'

  export default {
    name:"${entity.name}",
    components: {  },
    data() {
      return{
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 }
        },
        buttonCol: {
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 12, offset: 5 }
          }
        },
        pageTitle: '新增${entity.displayName}',
        loading: false,
        isSubmitLoading: false,
        form: this.$form.createForm(this),
        id: 0,
        rules:{}
      }
    },
    methods: {
      handleSubmit(e) {
        e.preventDefault()
        this.form.validateFields(async (err, values) => {
          if(!err){
            let resp = await saveOne({entity:Object.assign({id: this.id},values)})
            if(this.validResp(resp)){
              this.$message.success(resp.msg);
              this.$router.push({name: '${entity.name}List'});
            }
          }
        })
      },
      async loadModel(){
        const { form } = this
        let id  =   this.$route.query.id || 0
        if(id > 0){
          this.id  = id
          let resp = await getDetail({id:id})
          if(this.validResp(resp)){
            this.pageTitle = "编辑${entity.displayName}"
            let keys = [
            ## for(item in entity.generalList) {
              ## if(item.isId == true){continue;}
              ## if(item.name == "deleted" ){continue;}
              ## if(item.name == "createAt" ){continue;}
              ## if(item.name == "creatorId" ){continue;}
              ## if(item.name == "modifyAt" ){continue;}
              ## if(item.name == "modifierId" ){continue;}
              '${item.name}',
            ## }
            ]
            let formData = pick(resp.data.model, keys)
            form.setFieldsValue(formData)
          }
        }else {
        }
      },
    },
    mounted() {
      this.loadModel()
    }
  }
</script>
